//
//  KFDAvatarViewController.m
//  KFD
//
//  Created by Lucky Luke on 8/25/15.
//  Copyright © 2015 kfd. All rights reserved.
//

#import "KFDAvatarViewController.h"
#import "KFDClient.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "ui_config.h"
#import "JTToast.h"
#import "KFDAccountManager.h"

@interface KFDAvatarViewController ()

@property NSArray* avatarList;

@end

@implementation KFDAvatarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.uiConfigurator configureNavigationBarWithBackAction:@selector(goBack)];
    self.title=@"选择头像";
    KFDClient* client=[KFDClient sharedClient];
    [client getAvatarListWithSuccessHandler:^(NSArray *avatarList) {
        self.avatarList=avatarList;
        dispatch_async(dispatch_get_main_queue(), ^{
            CGFloat width=SCREEN_WIDTH/4;
            for (int i=0; i<avatarList.count; i++) {
                UIImageView* avatar=[[UIImageView alloc] initWithFrame:CGRectMake((i%4)*width, (i/4)*width+70, width, width)];
                avatar.tag=i;
                [avatar sd_setImageWithURL:avatarList[i]];
                [self.view addSubview:avatar];
                UITapGestureRecognizer* tapRecog=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(avatarTapped:)];
                [avatar addGestureRecognizer:tapRecog];
                avatar.userInteractionEnabled=YES;
            }
        });
    } failureHandler:^(KFDResponseBody *response) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [[JTToast makeText:response.message] showInView:self.view size:CGSizeMake(response.message.length*20, 40)];
        });
    }];
}

-(void)avatarTapped:(UITapGestureRecognizer*)tapRecog
{
    UIView* avatar=tapRecog.view;
    avatar.alpha=0.5;
    if (self.avatarList&&self.avatarList.count>avatar.tag) {
        NSString* url=self.avatarList[avatar.tag];
        KFDAccountManager* manager=[KFDAccountManager sharedManager];
        [manager updateAvatar:url successHandler:^{
            dispatch_async(dispatch_get_main_queue(), ^{
                [self goBack];
            });
        } failureHandler:^(KFDResponseBody *response) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [[JTToast makeText:response.message]  showInView:self.view size:CGSizeMake(response.message.length*20, 40)];
            });
        }];
    }
}

-(void)goBack
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
